package com.jyh.blog.controller;

import com.jyh.blog.annotation.OptLog;
import com.jyh.blog.dto.RoleDTO;
import com.jyh.blog.dto.UserRoleDTO;
import com.jyh.blog.service.RoleService;
import com.jyh.blog.utils.Result;
import com.jyh.blog.vo.ConditionVO;
import com.jyh.blog.vo.RoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.jyh.blog.constant.OptTypeConst.REMOVE;
import static com.jyh.blog.constant.OptTypeConst.SAVE_OR_UPDATE;

@Api(tags = "权限管理")
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 查询用户角色列表
     * 在用户管理中用
     * @return
     */
    @ApiOperation(value = "查询用户角色列表")
    @GetMapping("admin/user/role")
    public Result<List<UserRoleDTO>> getUserRoleList(){
        return Result.ok(roleService.getUserRoleList());
    }

    /**
     * 条件查询角色列表
     * @param conditionVO
     * @return
     */
    @ApiOperation(value = "条件查询角色列表")
    @GetMapping("admin/roles")
    public Result<List<RoleDTO>> roleList(ConditionVO conditionVO){
        return Result.ok(roleService.roleList(conditionVO));
    }

    /**
     * 添加或修改角色
     * @param roleVO
     * @return
     */
    @OptLog(optType = SAVE_OR_UPDATE)
    @ApiOperation(value = "添加或修改角色")
    @PostMapping("admin/roles")
    public Result<String> saveOrUpdateRole(@RequestBody @Valid RoleVO roleVO){
        roleService.saveOrUpdateRole(roleVO);
        return Result.ok();
    }

    /**
     * 删除用户角色
     * @param idList
     * @return
     */
    @OptLog(optType = REMOVE)
    @ApiOperation(value = "删除用户角色")
    @DeleteMapping("admin/roles")
    public Result<String> deleteRoles(@RequestBody List<Integer> idList ){
        roleService.deleteRoles(idList);
        return Result.ok();
    }

}
